package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbRolePermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbRolePermissionDao extends JpaRepository<TbRolePermission, Integer> {


	TbRolePermission getByRoleIdAndPermissionId(Integer roleId, Integer permissionId);

	@Query("select rs.id from TbRolePermission rs where rs.roleId=:roleId")
	List<Integer> findAllIdByRoleId(@Param("roleId") Integer roleId);

	@Query("select rs.id from TbRolePermission rs where rs.roleId=:roleId and rs.id not in(:idList)")
	List<Integer> findAllDelId(@Param("roleId") Integer roleId, @Param("idList") List<Integer> idList);

	@Modifying
	@Query("delete from TbRolePermission rs where rs.id in(:idList)")
	void deleteByIdList(@Param("idList") List<Integer> idList);


	@Query("select rp.permissionId from TbRolePermission rp where rp.roleId=:roleId")
	List<Integer> findPermissionIdByRoleId(@Param("roleId") Integer roleId);

	@Query("select rp.permissionId " +
			"from TbRolePermission rp " +
			" inner join TbPermission p on rp.permissionId=p.id " +
			" where rp.roleId in(:roleIdList) and p.status=:status")
	List<Integer> findPermissionIdByRoleIdList(@Param("roleIdList") List<Integer> roleIdList, @Param("status") String status);

}
